/**************************************************************

Copyright Astrium Services GEO-Information / Spot Image
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

 ***************************************************************/
package com.spotimage.catalog.client.ui.tab;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.JsArray;
import com.smartgwt.client.data.RecordList;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tab.Tab;
import com.spotimage.catalog.client.event.MoveToSelectionEvent;
import com.spotimage.catalog.client.event.EventBus;
import com.spotimage.catalog.client.event.RemoveFromSearchResultEvent;
import com.spotimage.catalog.client.event.SearchResultEvent;
import com.spotimage.catalog.client.event.SelectedSearchResultFootprintEvent;
import com.spotimage.catalog.client.event.DeselectedSearchResultFootprintEvent;
import com.spotimage.catalog.client.json.JSONScene;
import com.spotimage.catalog.client.json.JSONSearchResult;
import com.spotimage.catalog.client.ui.Ui;
import com.spotimage.catalog.client.ui.listgrid.SceneListGridRecord;
import com.spotimage.catalog.client.ui.listgrid.SearcResultListGrid;
import com.spotimage.catalog.client.util.Util;

/**
 * Search result tab
 */
public class SearchResultTab extends Tab implements SearchResultEvent.Handler, 
													SelectedSearchResultFootprintEvent.Handler, 
													DeselectedSearchResultFootprintEvent.Handler, 
													MoveToSelectionEvent.Handler,
													RemoveFromSearchResultEvent.Handler {
	
	/** tab title */
	private static final String TITLE = "Result";
	/** listGrid of search result's scenes */
	private SearcResultListGrid listGrid;

	/**
	 * Build UI.
	 */
	public SearchResultTab() {
		super(TITLE, "[SKIN]/RecordEditor/filter.png");
		
		EventBus.getInstance().addHandler(SearchResultEvent.TYPE, this);
		EventBus.getInstance().addHandler(SelectedSearchResultFootprintEvent.TYPE, this);
		EventBus.getInstance().addHandler(DeselectedSearchResultFootprintEvent.TYPE, this);
		EventBus.getInstance().addHandler(MoveToSelectionEvent.TYPE, this);
		EventBus.getInstance().addHandler(RemoveFromSearchResultEvent.TYPE, this);
		
		listGrid = new SearcResultListGrid();
		setTitle();
		
		VLayout layout = new VLayout(5);
		layout.setPadding(new Integer(5));
		layout.setBackgroundColor(Ui.BACKGROUND_COLOR);
		layout.setBorder("1px solid " + Ui.BORDER_COLOR);
		layout.addMember(listGrid);
		
		IButton keepButton = new IButton();
		keepButton.setTitle("Keep in selection");
		keepButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				SceneListGridRecord selectedRecord = (SceneListGridRecord) listGrid.getSelectedRecord();
				if (selectedRecord != null) {
					listGrid.removeData(selectedRecord);
					EventBus.getInstance().fireEvent(new MoveToSelectionEvent(selectedRecord));
				}
			}
		});
		layout.addMember(keepButton);
		
		setPane(layout);
	}
	
	/** 
	 * set title with search result scene items number.
	 * @param itemsNumber
	 */
	private void setTitle() {
		super.setTitle(TITLE + " (" + listGrid.getRecords().length + ")");
	}

	/**
	 * Receive {@link SearchResultEvent} and add seach result scene items to list.
	 */
	//@Override
	public void onSearchResult(SearchResultEvent e) {
		List<SceneListGridRecord> scenes = new ArrayList<SceneListGridRecord>();
		JSONSearchResult sr = e.getSearchResult();
		JsArray<JSONScene> resultScenes = sr.getScenes();
		for (int i = 0; i < resultScenes.length(); i++) {
			JSONScene scene = resultScenes.get(i);
			scenes.add(new SceneListGridRecord(scene.getId(), scene.getCloudCoverPercentage(), 
					Util.getDateFromISO8601(scene.getAcquisitionDateString()), scene.getImageUrl(), scene.getSatellite(),
					scene.getSnowCoverPercentage(), scene.getMetadataUrl(), scene.getShift(), scene.getMinShift(), scene.getMaxShift(), 
					scene.getArchivingStation(), scene.getUpperLeft(), scene.getUpperRight(), scene.getLowerLeft(), scene.getLowerRight()));
		}
		listGrid.populate(scenes);
		setTitle();
	}
	
	/**
	 * Receive {@link MoveToSelectionEvent} and update title.
	 */
	//@Override
	public void onMoveToSelection(MoveToSelectionEvent e) {
		setTitle();
	}
	
	/**
	 * Receive {@link SelectedSearchResultFootprintEvent} and select the scene items in list.
	 */
	//@Override
	public void onSelectedSearchResultFootprint(SelectedSearchResultFootprintEvent e) {
		RecordList recordList = listGrid.getRecordList();
		int foundRecordIndex = recordList.findIndex("id", e.getId());
		if (foundRecordIndex != -1) {
			listGrid.deselectAllRecords();
			listGrid.selectRecord(foundRecordIndex);
		} 
	}

	/**
	 * Receive {@link DeselectedSearchResultFootprintEvent} and deselect scene items in list.
	 */
	//@Override
	public void onDeselectedSearchResultFootprint(DeselectedSearchResultFootprintEvent e) {
		listGrid.deselectAllRecords();
	}
	
	/**
	 * Receive {@link RemoveFromSearchResultEvent} and remove scene items from search result.
	 */
	//@Override
	public void onRemoveFromSearchResult(RemoveFromSearchResultEvent e) {
		setTitle();
	}

}
